# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
from . import parser


import logging
_logger = logging.getLogger(__name__)


class TenderCategory(models.Model):
    _name = "tender.category"
    _description = "Tender Category"
    _order = "id"

    code = fields.Char(string='Code')
    name = fields.Char(string='Name')


class TenderWeb(models.Model):
    _name = "tender.web"
    _description = "Tender Web"
    _order = "id"

    name = fields.Char(string='Name')
    full_name = fields.Char(string='Full Name')
    url = fields.Char()
    crawler = fields.Char()

    category_id = fields.Many2one("tender.category", string='Category', ondelete='restrict' )
    category_name = fields.Char(string='Category Name', related='category_id.name' )
    city_id = fields.Many2one("res.city", string='City', ondelete='restrict', domain="[('state_id', '=?', state_id)]")
    state_id = fields.Many2one("res.country.state", string='State', ondelete='restrict', domain="[('country_id', '=?', country_id)]")
    country_id = fields.Many2one('res.country', string='Country', ondelete='restrict')

    project_ids = fields.One2many('tender.project', 'tender_web_id')


    @api.model
    def crawler_run(self):
        _logger.info('crawler start')
        webs = self.search([])
        webs.crawle()
        _logger.info('crawler stop')


    @api.multi
    def crawle(self):
        for rec in self:
            rec._crawle()
        
    def _crawle(self, page_no=None):

        crawler = self.crawler
        host = self.url
        
        #print('123123123123, 0 ')
        print( self.name, self.category_name)
        print( self.crawler, self.url)
        
        hp = parser.parsers[crawler](host)
        results = hp.parser(page_no=page_no)

        print('end, 2 ')
        #print('123123123123, 1 ', results)
        #print('123123123123, 2 ')
        
        for res in results[::-1]:
            
            name = res['title']
            url = res['href']
            date = res['date']
            
            prj = self.project_ids.search([
                ('tender_web_id','=',self.id),
                ('name','=',name),
                ('url','=',url),
                ('date','=',date),
            ], limit=1)
            
            if not prj:
                vals = {
                    'tender_web_id': self.id,
                    'name': name,
                    'url':  url ,
                    'date': date,
                    'city': res.get('city'),
                }
                
                
                self.project_ids.create(vals)


class TenderProject(models.Model):
    _name = "tender.project"
    _description = "Tender Project"
    _order = "date desc, id desc"

    name = fields.Char(string='Name')
    url = fields.Char()
    date = fields.Date()
    city = fields.Char(string='City Source' )
    
    tender_web_id = fields.Many2one("tender.web", string='Web', ondelete='restrict' )
    category_id = fields.Many2one("tender.category", string='Category', related='tender_web_id.category_id')
    category_name = fields.Char(string='Category Name', related='category_id.name' )
    city_id = fields.Many2one("res.city", string='City',related='tender_web_id.city_id' )
    state_id = fields.Many2one("res.country.state", string='State',related='tender_web_id.state_id')
    country_id = fields.Many2one('res.country', string='Country', related='tender_web_id.country_id')
    